//
//  PywBindSetPasswordController.m
//  PywSdk
//
//  Created by Jade on 16/8/30.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywBindSetPasswordController.h"
#import "PywTextFieldView.h"
#import "UIButton+Pyw.h"
#import "UILabel+Pyw.h"
#import "PywMasonry.h"
#import "PywBindSetPwdApi.h"
#import "PywMBProgressHUD+Pyw.h"
#import "ApiClientConst.h"
#import "PywUserViewController.h"
#import "PywBuriedManager.h"

@interface PywBindSetPasswordController ()
@property (nonatomic,strong) PywTextFieldView *textFieldView;
@property (nonatomic,strong) UIButton *sureButton;
@property (nonatomic, strong) UILabel *reminderLabel;
@property (nonatomic,strong) PywBindSetPwdApi *bindSetPwdApi;
@property (nonatomic,copy) NSString *passport;
@end

@implementation PywBindSetPasswordController
{
    BOOL _wasKeyBoardManagerEnabled;
}

- (instancetype)initWithPassport:(NSString *)passport
{
    self = [super init];
    if (self) {
        self.passport = passport;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setUp];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    NSLog(@"%s",__FUNCTION__);
    [PywBuriedManager manager].page_sn = @"6";
    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"6" actionSn:@"1" typeSn:@"1"];
    _wasKeyBoardManagerEnabled = [[IQKeyboardManager sharedManager] isEnabled];
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:NO];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
    [[IQKeyboardManager sharedManager] setKeyboardDistanceFromTextField:90];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    NSLog(@"%s",__FUNCTION__);
    [[IQKeyboardManager sharedManager] setEnable:_wasKeyBoardManagerEnabled];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%s",__FUNCTION__);
    [PywBuriedManager manager].page_sn = @"";
//    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"6" actionSn:@"2" typeSn:@"2"];
}
- (void)returnback
{
    [super returnback];
    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"6" actionSn:@"7" typeSn:@"3"];
}

#pragma mark - setUp
- (void)setUp
{
    self.title = @"设置手机登陆密码";
    self.navigationItem.rightBarButtonItem = nil;
    [self.view addSubview:self.textFieldView];
    [self.view addSubview:self.sureButton];
    [self.view addSubview:self.reminderLabel];
    [self addConstraints];
}

- (void)addConstraints
{
    WEAK_SELF;
    [self.textFieldView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view.mas_top).offset(80);
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.width.equalTo(weakSelf.view.mas_width).multipliedBy(0.85);
        make.height.mas_equalTo(40);
    }];
    
    [self.sureButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.equalTo(weakSelf.textFieldView.mas_bottom).offset(40);
        make.centerX.equalTo(weakSelf.textFieldView.mas_centerX);
        make.width.height.equalTo(weakSelf.textFieldView);
    }];
    
    [self.reminderLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(weakSelf.sureButton.mas_bottom).offset(20);
        make.width.equalTo(weakSelf.sureButton.mas_width);
    }];
}

#pragma mark - getter
- (PywBindSetPwdApi *)bindSetPwdApi
{
    if (!_bindSetPwdApi) {
        _bindSetPwdApi = [PywBindSetPwdApi manager];
    }
    return _bindSetPwdApi;
}

- (PywTextFieldView *)textFieldView
{
    if (!_textFieldView) {
        _textFieldView = [PywTextFieldView passwordTextFieldWithLeftImg:@"icon_password_normal"
                                                        TextPlaceholder:@"请输入6-12位登录密码"
                                                        ButtonNormalImg:@"icon_visible_normal"
                                                      ButtonSelectedImg:@"icon_visible_disabled"];
        [_textFieldView setAccessibilityId:@"binphone_phonepwsd"];
    }
    return _textFieldView;
}
- (UIButton *)sureButton
{
    if (!_sureButton) {
        _sureButton = [UIButton buttonWithBackgroundColor:UIColorFromRGB(0xffbc00)
                                             cornerRadius:20
                                               titleColor:[UIColor whiteColor]
                                       titleLabelFontSize:18
                                                    title:@"确定"];
        [_sureButton addTarget:self
                        action:@selector(selectSureButton:)
              forControlEvents:UIControlEventTouchUpInside];
        [_sureButton setAccessibilityIdentifier:@"binphone_surebtn"];
    }
    return _sureButton;
}
- (UILabel *)reminderLabel{
    if (!_reminderLabel) {
        _reminderLabel = [UILabel labelWithText:@"温馨提示：用手机登录可任意切换游戏账号"
                                  textAlignment:NSTextAlignmentCenter
                                       fontSize:12
                                      textColor:UIColorFromRGB(0x333333)];
    }
    return _reminderLabel;
}
#pragma mark - Actions

- (void)selectSureButton:(UIButton *)sender
{
    [self.view endEditing:YES];
    
    NSString *password = [self.textFieldView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (password.length == 0) {
        [PywMBProgressHUD showError:@"请输入密码！"];
    }else if (password.length < 6) {
        [PywMBProgressHUD showError:@"密码不能低于6位！"];
    }else {
        [self requestBindSetPwd];
    }
}

#pragma mark - Request

- (void)requestBindSetPwd
{
    NSString *password = [self.textFieldView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (password.length > 0) {
        WEAK_SELF;
        [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"6" actionSn:@"13" typeSn:@"3"];
        [self.bindSetPwdApi requestBindSetPwdWithPassport:self.passport password:password success:^(NSString *status, id anyObjct) {
            [PywMBProgressHUD showSuccess:@"绑定成功!"];
            [weakSelf returnBackAction];
        } fail:^(NSError *error) {
            
        }];
    }
}
#pragma mark - Actions
- (void)returnBackAction
{
    BOOL isPop = NO;
    for (UIViewController *viewController in self.navigationController.viewControllers) {
        if ([viewController isKindOfClass:[PywUserViewController class]]) {
            isPop = YES;
            break;
        }
    }
    if (isPop) {
        [[NSNotificationCenter defaultCenter] postNotificationName:ReloadUserCenterData object:nil];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else {
        [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
